package com.qd.core.service;

import com.qd.common.sys.domain.entity.dict.DictDataDTO;
import com.qd.common.sys.util.UserAgent;
import com.qd.core.dblog.LogProcess;
import com.qd.panda.service.third.etc.EtcSignUtil;
import com.qd.system.model.DictType;
import com.qd.system.service.dict.DictDataService;
import com.qd.system.service.dict.DictTypeService;
import com.qd.system.service.msg.server.MsgProcess;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 初始化
 *
 * @author sjk
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class InitSyncService {

    private final DictTypeService dictTypeService;
    private final DictDataService dictDataService;

    @Async
    public void init(){
        EtcSignUtil.init();
    }

    @Async
    @Transactional(readOnly = true)
    public void initDictData() {
        final long start = System.currentTimeMillis();
        dictDataService.clearCacheAll();
        final List<DictType> dictTypeList = dictTypeService.list();
        if (!dictTypeList.isEmpty()) {
            for (DictType dictType : dictTypeList) {
                dictDataService.getListByType(dictType.getTypeId());
            }
        }
        dictDataService.setRedisCache(1, new DictDataDTO());
        final long end = System.currentTimeMillis();
        log.info("加载码表信息耗时: {}毫秒", end - start);
    }

    @Async
    public void initMsgProcess() {
        MsgProcess.getInstance().startup();
        LogProcess.getInstance().startup();
    }

//    @Async
//    public void initDelayOrderQueueManager() {
//        DelayOrderQueueManager.getInstance().init();
//    }

    @Async
    public void initUserAgent(){
        UserAgent.init();
    }
}